﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Petaframe.Caching
{
    internal class CacheWrapper<TKey, TItem> : ICache<TKey, TItem> where TKey : IEquatable<TKey>
    {
        private ICache<TKey, TItem> mCache;
        private long mResetFilter = 0;
        public CacheWrapper(ICache<TKey, TItem> cache) : this(cache, 0) { }

        public CacheWrapper(ICache<TKey, TItem> cache, long resetFilter)
        {
            mResetFilter = resetFilter;
            mCache = cache;
        }
        #region ICache<TKey,TItem> Members

        public void Add(TKey key, TItem item, BaseToken token)
        {
            if (mResetFilter != 0)
                token.ResetFilter = mResetFilter;

            mCache.Add(key, item, token);
        }

        public void Add(TKey key, TItem item)
        {
            Add(key, item, new BaseToken());
        }

        public void AddIfNotExists(TKey key, Func<TItem> getvalues, BaseToken token)
        {
            if (mResetFilter != 0)
                token.ResetFilter = mResetFilter;
            mCache.AddIfNotExists(key, getvalues, token);
        }

        public void AddRange(Func<IEnumerable<KeyValuePair<TKey, TItem>>> getRange, BaseToken token)
        {
            if (mResetFilter != 0)
                token.ResetFilter = mResetFilter;
            mCache.AddRange(getRange, token);
        }

        public void AddRange(IEnumerable<KeyValuePair<TKey, TItem>> items, BaseToken token)
        {
            if (mResetFilter != 0)
                token.ResetFilter = mResetFilter;
            mCache.AddRange(items, token);
        }

        public void BeginAddRange()
        {
            mCache.BeginAddRange();
        }

        public bool ContainsKey(TKey key)
        {
            return mCache.ContainsKey(key);
        }

        public long Count()
        {
            return mCache.Count();
        }

        public void EndAddRange()
        {
            mCache.EndAddRange();
        }

        public TItem Get(TKey key)
        {
            return mCache.Get(key);
        }

        public TItem GetWithCreate(TKey key, Func<TItem> createItem, BaseToken token)
        {
            if (mResetFilter != 0)
                token.ResetFilter = mResetFilter;
            return mCache.GetWithCreate(key, createItem, token);
        }

        public void Remove(TKey key)
        {
            mCache.Remove(key);
        }

        public void Reset()
        {
            mCache.Reset();
        }

        public bool TryGet(TKey key, out TItem outItem)
        {
            return mCache.TryGet(key, out outItem);
        }

        #endregion

        #region ICache<TKey,TItem> Members


        public Action OnReset
        {
            get
            {
                return mCache.OnReset;
            }
            set
            {
                mCache.OnReset = value;
            }
        }

        #endregion
    }

}
